#include "attributesetting.h"
#include "ui_attributesetting.h"
#include "inquiresql.h"
#include <QMessageBox>
#include <QKeyEvent>

AttributeSetting::AttributeSetting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::AttributeSetting)
{
    ui->setupUi(this);
    setWindowTitle(tr("商品属性设置"));

    //设置自定义的鼠标指针图案
    QCursor *myCursor= new QCursor(QPixmap(":/Mouse/Image/mouse/mouse.png"),15,10);
    setCursor(*myCursor);

    pageChange = false;

//    QHeaderView* vheaderView = ui->tableWidgetSpecification->verticalHeader();
//    vheaderView->setHidden(true);
    ui->tableWidgetSpecification->verticalHeader()->setHidden(true);
    ui->tableWidgetBrands->verticalHeader()->setHidden(true);
    ui->tableWidgetColor->verticalHeader()->setHidden(true);
    ui->tableWidgetUnit->verticalHeader()->setHidden(true);

    //设置列宽可拖动
//    ui->tableWidgetSpecification->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetBrands->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetColor->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetUnit->horizontalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetSpecification->verticalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetBrands->verticalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetColor->verticalHeader()->setResizeMode(QHeaderView::Interactive);
//    ui->tableWidgetUnit->verticalHeader()->setResizeMode(QHeaderView::Interactive);

    ui->tableWidgetSpecification->setStyleSheet("\
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, \
        stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), \
        stop: 0.4 rgba(255, 255, 0, 30%), stop: 0.6 rgba(0, 255, 0, 30%), \
        stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); ");
    ui->tableWidgetBrands->setStyleSheet("\
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, \
        stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), \
        stop: 0.4 rgba(255, 255, 0, 30%), stop: 0.6 rgba(0, 255, 0, 30%), \
        stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); ");
    ui->tableWidgetColor->setStyleSheet("\
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, \
        stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), \
        stop: 0.4 rgba(255, 255, 0, 30%), stop: 0.6 rgba(0, 255, 0, 30%), \
        stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); ");
    ui->tableWidgetUnit->setStyleSheet("\
    background-color: qlineargradient(x1: 0, y1: 0, x2: 1, y2: 1, \
        stop: 0 rgba(255, 0, 0, 30%), stop: 0.2 rgba(255, 128, 0, 30%), \
        stop: 0.4 rgba(255, 255, 0, 30%), stop: 0.6 rgba(0, 255, 0, 30%), \
        stop: 0.8 rgba(0, 128, 255, 30%), stop: 1 rgba(128, 0, 255, 30%)); ");

    ui->tabWidget->setStyleSheet("\
        QTabBar::tab {\
            color: rgb(84,2,119);\
            background-image: url(:/attribute/Image/attribute/wood.jpg); \
            border: 2px solid rgb(68,66,64);\
            border-bottom-color: rgb(68,66,64); \
            border-top-left-radius: 24px;\
            border-top-right-radius: 24px;\
            max-height: 25px;\
            max-width: 20px;\
            min-width: 10ex;\
            padding: 2px;\
            } \
        QTabWidget::tab-bar {\
            alignment: center;\
            } \
        QTabBar::tab:!selected {\
            margin-top: 5px; \
            }\
        QTabBar::tab:selected {\
            color: rgb(255,0,128); \
            }\
        ");

    ui->tableWidgetSpecification->setColumnWidth(0,100);
    ui->tableWidgetSpecification->setColumnWidth(1,228);
    ui->tableWidgetBrands->setColumnWidth(0,100);
    ui->tableWidgetBrands->setColumnWidth(1,228);
    ui->tableWidgetColor->setColumnWidth(0,100);
    ui->tableWidgetColor->setColumnWidth(1,228);
    ui->tableWidgetUnit->setColumnWidth(0,100);
    ui->tableWidgetUnit->setColumnWidth(1,228);
}

AttributeSetting::~AttributeSetting()
{
    delete ui;
}

void AttributeSetting::displayAttributePage(const int &currentAttributePage)
{
    attributelist list;
    list.clear();

    switch (currentAttributePage)
    {
        case SpecificationSetting:
        {
            m_currentPage = SpecificationSetting;
            ui->tabWidget->setCurrentIndex(SpecificationSetting);
            pageChange = true;
            ui->tableWidgetSpecification->clear();
            ui->tableWidgetSpecification->setFocus();

            QStringList headers;
            headers << tr("编号") << tr("规格");
            ui->tableWidgetSpecification->setColumnCount(2);
            ui->tableWidgetSpecification->setHorizontalHeaderLabels(headers);
            for (int i = 0; i < 2; ++i)
            {
                ui->tableWidgetSpecification->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
                ui->tableWidgetSpecification->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
            }

            list = m_sql->InquireAttribute(SpecificationSetting);
            ui->tableWidgetSpecification->setRowCount(list.count());
            for(int i = 0; i < list.count(); ++i)
            {
                QTableWidgetItem *attribute_number = new QTableWidgetItem(list.at(i).get_attribute_number());
                QTableWidgetItem *attribute = new QTableWidgetItem(list.at(i).get_attribute());

                ui->tableWidgetSpecification->setItem(i, 0, attribute_number);
                ui->tableWidgetSpecification->setItem(i, 1, attribute);

                //设置对齐方式
                attribute_number->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
                attribute->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

                //设置文字颜色
                attribute->setTextColor(QColor(255, 0, 255));

                attribute_number->setFont(QFont("DejaVu Sans", 12, 2, false));
                attribute->setFont(QFont("DejaVu Sans", 12, 2, false));
            }

            ui->tableWidgetSpecification->setCurrentItem(
                ui->tableWidgetSpecification->item(list.count() - 1 , 1));
            ui->lineEditCommoditySpecification->setText(list.at(list.count() - 1).get_attribute());
            for (int i = 0; i < ui->tableWidgetSpecification->rowCount(); ++i)
            {
                if (i % 2 == 0)
                {
                    for (int j = 0; j < ui->tableWidgetSpecification->columnCount(); ++j)
                    {
                        QTableWidgetItem *item = ui->tableWidgetSpecification->item(i,j);
                        if (item)
                        {
                            const QColor color = QColor(252,222,156);
                            item->setBackgroundColor(color);
                        }
                    }
                }
            }
            break;
        }
        case BrandsSetting:
        {
            m_currentPage = BrandsSetting;
            ui->tabWidget->setCurrentIndex(BrandsSetting);
            pageChange = true;
            ui->tableWidgetBrands->clear();
            ui->tableWidgetBrands->setFocus();

            QStringList headers;
            headers << tr("编号") << tr("品牌");
            ui->tableWidgetBrands->setColumnCount(2);
            ui->tableWidgetBrands->setHorizontalHeaderLabels(headers);
            for (int i = 0; i < 2; ++i)
            {
                ui->tableWidgetBrands->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
                ui->tableWidgetBrands->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
            }

            list = m_sql->InquireAttribute(BrandsSetting);
            ui->tableWidgetBrands->setRowCount(list.count());
            for(int i = 0; i < list.count(); ++i)
            {
                QTableWidgetItem *attribute_number = new QTableWidgetItem(list.at(i).get_attribute_number());
                QTableWidgetItem *attribute = new QTableWidgetItem(list.at(i).get_attribute());

                ui->tableWidgetBrands->setItem(i, 0, attribute_number);
                ui->tableWidgetBrands->setItem(i, 1, attribute);

                //设置对齐方式
                attribute_number->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
                attribute->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

                //设置文字颜色
                attribute->setTextColor(QColor(255, 0, 255));

                attribute_number->setFont(QFont("DejaVu Sans", 12, 2, false));
                attribute->setFont(QFont("DejaVu Sans", 12, 2, false));
            }
            ui->tableWidgetBrands->setCurrentItem(
                ui->tableWidgetBrands->item(list.count() - 1 , 1));
            ui->lineEditCommodityBrands->setText(list.at(list.count() - 1).get_attribute());
            for (int i = 0; i < ui->tableWidgetBrands->rowCount(); ++i)
            {
                if (i % 2 == 0)
                {
                    for (int j = 0; j < ui->tableWidgetBrands->columnCount(); ++j)
                    {
                        QTableWidgetItem *item = ui->tableWidgetBrands->item(i,j);
                        if (item)
                        {
                            const QColor color = QColor(252,222,156);
                            item->setBackgroundColor(color);
                        }
                    }
                }
            }
            break;
        }
        case ColorSetting:
        {
            m_currentPage = ColorSetting;
            ui->tabWidget->setCurrentIndex(ColorSetting);
            pageChange = true;
            ui->tableWidgetColor->clear();
            ui->tableWidgetColor->setFocus();

            QStringList headers;
            headers << tr("编号") << tr("颜色");
            ui->tableWidgetColor->setColumnCount(2);
            ui->tableWidgetColor->setHorizontalHeaderLabels(headers);
            for (int i = 0; i < 2; ++i)
            {
                ui->tableWidgetColor->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
                ui->tableWidgetColor->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
            }

            list = m_sql->InquireAttribute(ColorSetting);
            ui->tableWidgetColor->setRowCount(list.count());
            for(int i = 0; i < list.count(); ++i)
            {
                QTableWidgetItem *attribute_number = new QTableWidgetItem(list.at(i).get_attribute_number());
                QTableWidgetItem *attribute = new QTableWidgetItem(list.at(i).get_attribute());

                ui->tableWidgetColor->setItem(i, 0, attribute_number);
                ui->tableWidgetColor->setItem(i, 1, attribute);

                //设置对齐方式
                attribute_number->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
                attribute->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

                //设置文字颜色
                attribute->setTextColor(QColor(255, 0, 255));

                attribute_number->setFont(QFont("DejaVu Sans", 12, 2, false));
                attribute->setFont(QFont("DejaVu Sans", 12, 2, false));
            }
            ui->tableWidgetColor->setCurrentItem(
                ui->tableWidgetColor->item(list.count() - 1 , 1));
            ui->lineEditCommodityColor->setText(list.at(list.count() - 1).get_attribute());
            for (int i = 0; i < ui->tableWidgetColor->rowCount(); ++i)
            {
                if (i % 2 == 0)
                {
                    for (int j = 0; j < ui->tableWidgetColor->columnCount(); ++j)
                    {
                        QTableWidgetItem *item = ui->tableWidgetColor->item(i,j);
                        if (item)
                        {
                            const QColor color = QColor(252,222,156);
                            item->setBackgroundColor(color);
                        }
                    }
                }
            }
            break;
        }
        case UnitSetting:
        {
            m_currentPage = UnitSetting;
            ui->tabWidget->setCurrentIndex(UnitSetting);
            pageChange = true;
            ui->tableWidgetUnit->clear();
            ui->tableWidgetUnit->setFocus();

            QStringList headers;
            headers << tr("编号") << tr("单位");
            ui->tableWidgetUnit->setColumnCount(2);
            ui->tableWidgetUnit->setHorizontalHeaderLabels(headers);
            for (int i = 0; i < 2; ++i)
            {
                ui->tableWidgetUnit->horizontalHeaderItem(i)->setFont(QFont("Helvetica", 12, 2, false));
                ui->tableWidgetUnit->horizontalHeaderItem(i)->setTextColor(QColor(200,111,30));
            }

            list = m_sql->InquireAttribute(UnitSetting);
            ui->tableWidgetUnit->setRowCount(list.count());
            for(int i = 0; i < list.count(); ++i)
            {
                QTableWidgetItem *attribute_number = new QTableWidgetItem(list.at(i).get_attribute_number());
                QTableWidgetItem *attribute = new QTableWidgetItem(list.at(i).get_attribute());

                ui->tableWidgetUnit->setItem(i, 0, attribute_number);
                ui->tableWidgetUnit->setItem(i, 1, attribute);

                //设置对齐方式
                attribute_number->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );
                attribute->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter );

                //设置文字颜色
                attribute->setTextColor(QColor(255, 0, 255));

                attribute_number->setFont(QFont("DejaVu Sans", 12, 2, false));
                attribute->setFont(QFont("DejaVu Sans", 12, 2, false));
            }
            ui->tableWidgetUnit->setCurrentItem(
                ui->tableWidgetUnit->item(list.count() - 1 , 1));
            ui->lineEditCommodityUnit->setText(list.at(list.count() - 1).get_attribute());
            for (int i = 0; i < ui->tableWidgetUnit->rowCount(); ++i)
            {
                if (i % 2 == 0)
                {
                    for (int j = 0; j < ui->tableWidgetUnit->columnCount(); ++j)
                    {
                        QTableWidgetItem *item = ui->tableWidgetUnit->item(i,j);
                        if (item)
                        {
                            const QColor color = QColor(252,222,156);
                            item->setBackgroundColor(color);
                        }
                    }
                }
            }
            break;
        }
        default:
            break;
    }

//    ui->tableWidgetAttribute->clear();
//    ui->tableWidgetAttribute->setFocus();
//    attributelist list;
//    list.clear();

//    if (AttributeName == tr("规格设置"))
//    {
//        current_page = SpecificationPage;

//        QStringList headers;
//        headers << tr("编号") << tr("规格");
//        ui->tableWidgetAttribute->setHorizontalHeaderLabels(headers);

//        list = m_sql->InquireAttribute(current_page);
//        ui->tableWidgetAttribute->setRowCount(list.count());
//    }
//    if (AttributeName == tr("品牌设置"))
//    {
//        current_page = BrandPage;

//        QStringList headers;
//        headers << tr("编号") << tr("品牌");
//        ui->tableWidgetAttribute->setHorizontalHeaderLabels(headers);

//        list = m_sql->InquireAttribute(current_page);
//        ui->tableWidgetAttribute->setRowCount(list.count());
//    }
//    if (AttributeName == tr("颜色设置"))
//    {
//        current_page = ColorPage;

//        QStringList headers;
//        headers << tr("编号") << tr("颜色");
//        ui->tableWidgetAttribute->setHorizontalHeaderLabels(headers);

//        list = m_sql->InquireAttribute(current_page);
//        ui->tableWidgetAttribute->setRowCount(list.count());
//    }
//    if (AttributeName == tr("单位设置"))
//    {
//        current_page = UnitPage;

//        QStringList headers;
//        headers << tr("编号") << tr("单位");
//        ui->tableWidgetAttribute->setHorizontalHeaderLabels(headers);

//        list = m_sql->InquireAttribute(current_page);
//        ui->tableWidgetAttribute->setRowCount(list.count());
//    }

//    //显示属性信息
//    for(int i = 0; i < list.count(); ++i)
//    {

//        QTableWidgetItem *attribute_number = new QTableWidgetItem(list.at(i).get_attribute_number());
//        QTableWidgetItem *attribute = new QTableWidgetItem(list.at(i).get_attribute());

//        ui->tableWidgetAttribute->setItem(i, 0, attribute_number);
//        ui->tableWidgetAttribute->setItem(i, 1, attribute);
//    }
    //ui->tableWidgetAttribute->scrollToItem(ui->tableWidgetAttribute->item( list.count()-1 , 1));
//    ui->tableWidgetAttribute->setCurrentItem(ui->tableWidgetAttribute->item( list.count() - 1 , 1));
//    ui->lineEditCommodityAttribute->setText(list.at(list.count() - 1).get_attribute());
//    setTableWidgetColor();
}

void AttributeSetting::on_tabWidget_currentChanged(int index)
{
    switch (index)
    {
        case SpecificationSetting:
            displayAttributePage(SpecificationSetting);
            break;
        case BrandsSetting:
            displayAttributePage(BrandsSetting);
            break;
        case ColorSetting:
            displayAttributePage(ColorSetting);
            break;
        case UnitSetting:
            displayAttributePage(UnitSetting);
            break;
        default:
            break;
    }
}

void AttributeSetting::on_pushButtonNew_clicked()
{
    switch (m_currentPage)
    {
        case SpecificationSetting:
        {
            if (ui->lineEditCommoditySpecification->text().isEmpty())
            {
                QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n请输入商品规格!") );
                return ;
            }
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            for(int i = 0; i < list.count(); ++i)
            {
                if (ui->lineEditCommoditySpecification->text() == list.at(i).get_attribute())
                {
                    QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                         tr("\n此规格已存在,不需要重新设置!") );
                    return;
                }
            }

            //向数据库中插入类别

            int attribute_number = list.at(list.count() - 1).get_attribute_number().toInt() + 1;

            CommodityAttribute attribute(QString::number(attribute_number).rightJustified(4, '0'),
                                    ui->lineEditCommoditySpecification->text());

            m_sql->InsertAttribute(m_currentPage, attribute);
            displayAttributePage(SpecificationSetting);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case BrandsSetting:
        {
            if (ui->lineEditCommoditySpecification->text().isEmpty())
            {
                QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n请输入商品品牌!") );
                return ;
            }
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            for(int i = 0; i < list.count(); ++i)
            {
                if (ui->lineEditCommodityBrands->text() == list.at(i).get_attribute())
                {
                    QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                         tr("\n此品牌已存在,不需要重新设置!") );
                    return;
                }
            }

            //向数据库中插入类别

            int attribute_number = list.at(list.count() - 1).get_attribute_number().toInt() + 1;

            CommodityAttribute attribute(QString::number(attribute_number).rightJustified(4, '0'),
                                    ui->lineEditCommodityBrands->text());

            m_sql->InsertAttribute(m_currentPage, attribute);
            displayAttributePage(BrandsSetting);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case ColorSetting:
        {
            if (ui->lineEditCommoditySpecification->text().isEmpty())
            {
                QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n请输入商品颜色!") );
                return ;
            }
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            for(int i = 0; i < list.count(); ++i)
            {
                if (ui->lineEditCommodityColor->text() == list.at(i).get_attribute())
                {
                    QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                         tr("\n此颜色已存在,不需要重新设置!") );
                    return;
                }
            }

            //向数据库中插入类别

            int attribute_number = list.at(list.count() - 1).get_attribute_number().toInt() + 1;

            CommodityAttribute attribute(QString::number(attribute_number).rightJustified(4, '0'),
                                    ui->lineEditCommodityColor->text());

            m_sql->InsertAttribute(m_currentPage, attribute);
            displayAttributePage(ColorSetting);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case UnitSetting:
        {
            if (ui->lineEditCommoditySpecification->text().isEmpty())
            {
                QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n请输入商品单位!") );
                return ;
            }
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            for(int i = 0; i < list.count(); ++i)
            {
                if (ui->lineEditCommodityUnit->text() == list.at(i).get_attribute())
                {
                    QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                         tr("\n此单位已存在,不需要重新设置!") );
                    return;
                }
            }

            //向数据库中插入类别

            int attribute_number = list.at(list.count() - 1).get_attribute_number().toInt() + 1;

            CommodityAttribute attribute(QString::number(attribute_number).rightJustified(4, '0'),
                                    ui->lineEditCommodityUnit->text());

            m_sql->InsertAttribute(m_currentPage, attribute);
            displayAttributePage(UnitSetting);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        default:
            break;
    }
}

void AttributeSetting::on_pushButtonModify_clicked()
{
    switch (m_currentPage)
    {
        case SpecificationSetting:
        {
            if (ui->lineEditCommoditySpecification->text().isEmpty())
            {
                QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n请输入商品规格!") );
                return ;
            }
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            for(int i = 0; i < list.count(); ++i)
            {
                if (ui->lineEditCommoditySpecification->text() == list.at(i).get_attribute())
                {
                    QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                         tr("\n此规格已存在,无法修改!") );
                    return;
                }
            }

            //向数据库中插入规格
            CommodityAttribute attribute(list.at(
                ui->tableWidgetSpecification->currentRow()).get_attribute_number(),
                ui->lineEditCommoditySpecification->text());

            m_sql->UpdateAttribute(m_currentPage, attribute);
            displayAttributePage(SpecificationSetting);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case BrandsSetting:
        {
            if (ui->lineEditCommoditySpecification->text().isEmpty())
            {
                QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n请输入商品品牌!") );
                return ;
            }
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            for(int i = 0; i < list.count(); ++i)
            {
                if (ui->lineEditCommodityBrands->text() == list.at(i).get_attribute())
                {
                    QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                         tr("\n此品牌已存在,无法修改!") );
                    return;
                }
            }

            //向数据库中插入品牌
            CommodityAttribute attribute(list.at(
                    ui->tableWidgetBrands->currentRow()).get_attribute_number(),
                    ui->lineEditCommodityBrands->text());

            m_sql->UpdateAttribute(m_currentPage, attribute);
            displayAttributePage(BrandsSetting);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case ColorSetting:
        {
            if (ui->lineEditCommoditySpecification->text().isEmpty())
            {
                QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n请输入商品颜色!") );
                return ;
            }
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            for(int i = 0; i < list.count(); ++i)
            {
                if (ui->lineEditCommodityColor->text() == list.at(i).get_attribute())
                {
                    QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                         tr("\n此颜色已存在,无法修改!") );
                    return;
                }
            }

            //向数据库中插入颜色
            CommodityAttribute attribute(list.at(
                    ui->tableWidgetColor->currentRow()).get_attribute_number(),
                    ui->lineEditCommodityColor->text());

            m_sql->UpdateAttribute(m_currentPage, attribute);
            displayAttributePage(ColorSetting);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case UnitSetting:
        {
            if (ui->lineEditCommoditySpecification->text().isEmpty())
            {
                QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n请输入商品单位!") );
                return ;
            }
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            for(int i = 0; i < list.count(); ++i)
            {
                if (ui->lineEditCommodityUnit->text() == list.at(i).get_attribute())
                {
                    QMessageBox::information(this, tr("(☆_☆)温馨提示 ..."),
                                         tr("\n此单位已存在,无法修改!") );
                    return;
                }
            }

            //向数据库中插入单位
            CommodityAttribute attribute(list.at(
                    ui->tableWidgetUnit->currentRow()).get_attribute_number(),
                    ui->lineEditCommodityUnit->text());

            m_sql->UpdateAttribute(m_currentPage, attribute);
            displayAttributePage(UnitSetting);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        default:
            break;
    }
}

void AttributeSetting::on_pushButtonDelete_clicked()
{
    switch (m_currentPage)

    {
        case SpecificationSetting:
        {
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            if (m_sql->AttributeIsUsed(m_currentPage,
                                       list.at(ui->tableWidgetSpecification->currentRow())
                                       .get_attribute_number()))
            {
                QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n该规格已被使用,无法删除"));
                return;
            }

            m_sql->DeleteAttribute(m_currentPage,
                    list.at(ui->tableWidgetSpecification->currentRow()).get_attribute_number());
            displayAttributePage(m_currentPage);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case BrandsSetting:
        {
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            if (m_sql->AttributeIsUsed(m_currentPage,
                                       list.at(ui->tableWidgetBrands->currentRow())
                                       .get_attribute_number()))
            {
                QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n该品牌已被使用,无法删除"));
                return;
            }

            m_sql->DeleteAttribute(m_currentPage,
                    list.at(ui->tableWidgetBrands->currentRow()).get_attribute_number());
            displayAttributePage(m_currentPage);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case ColorSetting:
        {
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            if (m_sql->AttributeIsUsed(m_currentPage,
                                       list.at(ui->tableWidgetColor->currentRow())
                                       .get_attribute_number()))
            {
                QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n该颜色已被使用,无法删除"));
                return;
            }

            m_sql->DeleteAttribute(m_currentPage,
                    list.at(ui->tableWidgetColor->currentRow()).get_attribute_number());
            displayAttributePage(m_currentPage);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        case UnitSetting:
        {
            const attributelist list = m_sql->InquireAttribute(m_currentPage);

            if (m_sql->AttributeIsUsed(m_currentPage,
                                       list.at(ui->tableWidgetUnit->currentRow())
                                       .get_attribute_number()))
            {
                QMessageBox::warning(this, tr("(☆_☆)温馨提示 ..."),
                                     tr("\n该单位已被使用,无法删除"));
                return;
            }

            m_sql->DeleteAttribute(m_currentPage,
                    list.at(ui->tableWidgetUnit->currentRow()).get_attribute_number());
            displayAttributePage(m_currentPage);
            emit signalUpdateComboBoxAttribute(m_currentPage);
            break;
        }
        default:
            break;
    }
}

void AttributeSetting::keyPressEvent(QKeyEvent *ev)
{
    switch(ev->key())
    {
        case Qt::Key_Escape:
            this->hide();
            break;
    default:
        QWidget::keyPressEvent(ev);
    }
}

void AttributeSetting::on_tableWidgetSpecification_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *)
{
    if (pageChange)
    {
        pageChange = false;
        return;
    }

    ui->lineEditCommoditySpecification->setText(current->text());
}

void AttributeSetting::on_tableWidgetBrands_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *)
{
    if (pageChange)
    {
        pageChange = false;
        return;
    }

    ui->lineEditCommodityBrands->setText(current->text());
}

void AttributeSetting::on_tableWidgetColor_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *)
{
    if (pageChange)
    {
        pageChange = false;
        return;
    }

    ui->lineEditCommodityColor->setText(current->text());
}

void AttributeSetting::on_tableWidgetUnit_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *)
{
    if (pageChange)
    {
        pageChange = false;
        return;
    }

    ui->lineEditCommodityUnit->setText(current->text());
}
